<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="加密串" prop="jhTraffic">
        <el-input v-model="queryParams.jhTraffic" clearable placeholder="请输入流量加密串" style="width: 240px" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="价格" prop="jhPrice">
        <el-input v-model="queryParams.jhPrice" clearable placeholder="请输入价格" style="width: 240px" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="jhEnabled">
        <el-select v-model="queryParams.jhEnabled" clearable placeholder="请输入状态" style="width: 240px">
          <el-option v-for="dict in dict.type.sys_jmc_status" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" end-placeholder="结束日期" range-separator="-" start-placeholder="开始日期" style="width: 240px" type="daterange"
                        value-format="yyyy-MM-dd"></el-date-picker>
      </el-form-item>
      <el-form-item label="购买用户" prop="jhSellClient">
        <el-select v-model="queryParams.jhSellClient" filterable placeholder="请选择购买用户">
          <!-- todo  -->
          <el-option v-for="item in userList" :key="item.userId" :label="item.userName" :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-form ref="elForm" :model="formData" :rules="rules" label-width="100px" size="medium">
      <el-row id="rowid">
        <el-form-item size="large">
          <el-button size="normal" type="primary" @click="handleAdd">生成流量加密串</el-button>
        </el-form-item>
      </el-row>
      <el-table v-loading="loading" :columns="columns" :data="sysTrafficRecordList" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="50"/>
        <el-table-column v-if="columns[0].visible" key="jhTraffic" align="center" label="流量加密串" prop="jhTraffic"/>
        <el-table-column v-if="columns[1].visible" key="jhPrice" :show-overflow-tooltip="true" align="center" label="价格（元）" prop="jhPrice"/>
        <el-table-column v-if="columns[2].visible" key="jhNum" :show-overflow-tooltip="true" align="center" label="数量" prop="jhNum"/>
        <el-table-column v-if="columns[3].visible" key="jhEnabled" :show-overflow-tooltip="true" align="center" label="状态" prop="jhEnabled"/>
        <el-table-column v-if="columns[4].visible" key="jhCreatedTime" align="center" label="创建时间" prop="jhCreatedTime" width="120"/>
        <el-table-column v-if="columns[5].visible" key="jhSellStatus" :show-overflow-tooltip="true" align="center" label="售卖状态" prop="jhSellStatus"/>
        <el-table-column v-if="columns[6].visible" key="jhSellClient" align="center" label="售卖用户" prop="jhSellClient">
          <template slot-scope="scope">
            <el-tag size="small">{{ userList.find(x => x.userId == scope.row.jhSellClient) ? userList.find(x => x.userId == scope.row.jhSellClient).userName : '' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[7].visible" align="center" label="操作" prop="jhCreatedTime" width="160">
          <template v-if="scope.row.jhTraffic !== 1" slot-scope="scope">
            <!--            <el-button size="mini" type="text" @click="handleUpdate(scope.row)">详情</el-button>-->
            <el-button size="mini" type="text" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button size="mini" type="text" @click="handleDisable(scope.row)">禁用</el-button>
            <el-button size="mini" type="text" @click="handleEnable(scope.row)">启用</el-button>
            <el-button size="mini" style="color: red" type="text" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total" @pagination="getList"/>
    </el-form>

    <!-- 添加流量串对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col>
            <el-form-item label="状态" prop="jhEnabled">
              <el-radio-group v-model="form.jhEnabled">
                <el-radio v-for="dict in dict.type.sys_jmc_status" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item label="流量加密串" prop="jhTraffic">
              <el-input v-model="form.jhTraffic" disabled placeholder="自动生成流量加密串，不可以修改"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item label="价格" prop="jhPrice">
              <el-input v-model="form.jhPrice" :disabled="isDisabled" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item label="数量" prop="jhNum">
              <el-input v-model="form.jhNum" :disabled="isDisabled" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item label="售卖给" prop="jhSellClient">
              <el-select v-model="form.jhSellClient" filterable placeholder="请输入">
                <el-option v-for="item in userList" :key="item.userId" :label="item.userName" :value="item.userId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import {
  addSysTrafficRecord,
  deleteSysTrafficRecord,
  disableSysTrafficRecord,
  enableSysTrafficRecord,
  getSysTrafficRecord,
  listTable,
  updateSysTrafficRecord
} from "@/api/jihuo/sysTrafficRecord";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import "splitpanes/dist/splitpanes.css";
import {listUser} from "@/api/system/user";

export default {
  dicts: ['sys_normal_disable', 'sys_jmc_status'],
  components: {Treeselect},
  props: [],
  data() {
    return {
      //加密串list数据
      sysTrafficRecordList: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 编辑时禁止修改
      isDisabled: false,
      // 用户列表
      userList: [],
      formData: {
        field101: undefined,
      },
      rules: {
        jhEnabled: [{required: true, message: "状态不能为空", trigger: "blur"}],
        jhPrice: [{required: true, message: "价格不能为空", trigger: "blur"}],
        jhNum: [{required: true, message: "数量不能为空", trigger: "blur"}]
      },
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jhTraffic: undefined,
        jhPrice: undefined,
        jhEnabled: undefined,
        jhSellClient: undefined
      },
      // 日期范围
      dateRange: "",
      // 列信息
      columns: [
        {key: 0, label: `流量加密串`, visible: true},
        {key: 1, label: `价格（元）`, visible: true},
        {key: 2, label: `数量`, visible: true},
        {key: 3, label: `状态`, visible: true},
        {key: 4, label: `创建时间`, visible: true},
        {key: 5, label: `售卖状态`, visible: true},
        {key: 6, label: `售卖用户`, visible: true},
        {key: 7, label: `操作`, visible: true}
      ]
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getList();
  },
  mounted() {
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm();
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.jhId !== undefined) {
            updateSysTrafficRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSysTrafficRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    resetForm() {
      this.$refs["queryForm"].resetFields()
    },
    getList() {
      this.loading = true;
      listTable(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          for (let i = 0; i < response.rows.length; i++) {
            if (response.rows[i].jhEnabled == "1") {
              response.rows[i].jhEnabled = "启用";
            } else {
              response.rows[i].jhEnabled = "禁用";
            }
            if (response.rows[i].jhSellStatus == "0") {
              response.rows[i].jhSellStatus = "已售卖";
            } else {
              response.rows[i].jhSellStatus = "未售卖";
            }
          }
          this.sysTrafficRecordList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
      listUser({}).then(response => {
        this.userList = response.rows;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.jhId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "生成加密串";
      this.isDisabled = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.jhId || this.ids;
      getSysTrafficRecord(id).then(response => {
        this.form = response.data;
        this.postOptions = response.posts;
        this.roleOptions = response.roles;
        this.open = true;
        this.title = "修改加密串信息";
        this.isDisabled = true;
      });
    },
    /** 禁用按钮操作 */
    handleDisable(row) {
      this.reset();
      const id = row.jhId || this.ids;
      this.$modal.confirm('是否禁用？').then(function () {
        return disableSysTrafficRecord(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("禁用成功");
      }).catch(() => {
      });
    },
    /** 启用按钮操作 */
    handleEnable(row) {
      this.reset();
      const id = row.jhId || this.ids;
      this.$modal.confirm('是否启用？').then(function () {
        return enableSysTrafficRecord(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("启用成功");
      }).catch(() => {
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.reset();
      const id = row.jhId || this.ids;
      this.$modal.confirm('是否删除？').then(function () {
        return deleteSysTrafficRecord(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
  }
}

</script>
<style>
</style>
